package com.jsbb.controller.tool;

import com.alibaba.fastjson.JSON;
import com.jsbb.common.Message;
import com.jsbb.config.ParamsConfig;
import com.jsbb.dto.tool.TableExportDto;
import com.jsbb.dto.tool.TableQueryDto;
import com.jsbb.service.tool.TableService;
import com.jsbb.util.PropertyUtils;
import com.jsbb.util.WordUtils;
import com.jsbb.vo.tool.TableListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 数据库表Controller
 *
 * @author jsbb
 */
@RestController
@RequestMapping("/tool/table")
@Tag(name = "2.2 工具管理-数据库表")
public class TableController {

    @Autowired
    private TableService tableService;

    @Autowired
    private ParamsConfig paramsConfig;

    @PostMapping("/list")
    @Operation(summary = "获取列表")
    public Message<List<TableListVo>> list(@RequestBody TableQueryDto queryDto) {
        if (!StringUtils.hasText(queryDto.getIp())) {
            queryDto.setIp(PropertyUtils.getDbIp());
        }
        if (!StringUtils.hasText(queryDto.getPort())) {
            queryDto.setPort(PropertyUtils.getDbPort());
        }
        if (!StringUtils.hasText(queryDto.getSchema())) {
            queryDto.setSchema(PropertyUtils.getDbSchema());
        }
        if (!StringUtils.hasText(queryDto.getUser())) {
            queryDto.setUser(PropertyUtils.getDbUser());
        }
        if (!StringUtils.hasText(queryDto.getPassword())) {
            queryDto.setPassword(PropertyUtils.getDbPassword());
        }
        List<TableListVo> pageVo = tableService.list(queryDto);
        return Message.success(pageVo);
    }

    @PostMapping("/export")
    @Operation(summary = "导出")
    public void export(@Validated @RequestBody TableExportDto exportDto, HttpServletResponse response) {
        if (!StringUtils.hasText(exportDto.getIp())) {
            exportDto.setIp(PropertyUtils.getDbIp());
        }
        if (!StringUtils.hasText(exportDto.getPort())) {
            exportDto.setPort(PropertyUtils.getDbPort());
        }
        if (!StringUtils.hasText(exportDto.getSchema())) {
            exportDto.setSchema(PropertyUtils.getDbSchema());
        }
        if (!StringUtils.hasText(exportDto.getUser())) {
            exportDto.setUser(PropertyUtils.getDbUser());
        }
        if (!StringUtils.hasText(exportDto.getPassword())) {
            exportDto.setPassword(PropertyUtils.getDbPassword());
        }
        Map<String, Object> dbInfo = JSON.parseObject(JSON.toJSONString(exportDto), Map.class);
        WordUtils.exportDbTable(dbInfo, response);
    }
}
